<?php


use Phinx\Seed\AbstractSeed;

class AdminUserSeed extends AbstractSeed
{
    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * https://book.cakephp.org/phinx/0/en/seeding.html
     */
    public function run()
    {
        // vendor/bin/phinx seed:run -s AdminUserSeed
        $model  = $this->table('admin_user');
        $fields = ['account', 'password', 'nickname', 'is_lock'];
        $values = [
            ['myadmin', 'myadmin', 'admin', 0]
        ];

        $insert = [];
        $count  = count($fields);
        foreach ($values as $key => $val){
            for($i=0; $i<$count; $i++){
                $insert[$key][$fields[$i]] = $val[$i];
            }
            $salt     = (string)mt_rand(100000, 999999);
            $password = hash('sha256', $insert[$key]['password'].$salt);

            $insert[$key]['salt']       = $salt;
            $insert[$key]['password']   = $password;
            $insert[$key]['created_at'] = date('Y-m-d H:i:s');
            $insert[$key]['updated_at'] = date('Y-m-d H:i:s');
        }

        if (empty($insert)) return false;

        $model->truncate();
        $model->insert($insert)->saveData();
    }
}
